{include file="./public/header" title="分配权限"/}
<link href="https://cdn.bootcss.com/bootstrap-validator/0.5.3/css/bootstrapValidator.min.css" rel="stylesheet">
<body class="hold-transition skin-blue fixed sidebar-mini">
<div class="wrapper">

    {include file="./public/navi"/}

    <!--页面主体-->
    <div class="content-wrapper">
        <section class="content-header">
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 后台</a></li>
                <li class="">用户组</li>
                <li class="active">分配权限</li>
            </ol>
        </section>
        <section class="content">

            <div class="box box-primary">
                <div class="box-header with-border">
                    <div class="box-title">分配权限</div>
                    <div class="box-tools">
                        <button onclick="javascript:history.back(-1);" type="button"
                                class="btn btn-box-tool"><i class="fa fa-chevron-left"></i> 返回
                        </button>
                        <button onclick="javascript:location.reload();" type="button"
                                class="btn btn-box-tool"><i class="fa fa-refresh"></i> 刷新
                        </button>
                        <button onclick="document.getElementById('form').submit();" type="submit"
                                class="btn btn-box-tool"><i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
                <div class="box-body">
                    <div class="nav-tabs-custom">
                        <ul class="nav nav-tabs">
                            <li class=""><a href="{:url('/admin/auth/group/edit',array('id'=>$authGroups.id))}">编辑用户组</a></li>
                            <li class="active"><a href="{:url('/admin/auth/group/assignrule',array('id'=>$authGroups.id))}">权限分配</a></li>
                        </ul>
                    </div>
                    <div class="table-responsive">
                        <form id="form" action="" method="post">
                            <input type="hidden" value="{$authGroups.id}" name="id">
                            <table id="table" class="table table-condensed table-striped table-hover">
                                <thead class="bordered-darkorange">
                                <tr>
                                    <th>
                                        <label style="padding:0 15px 0 0;">
                                            <input id="chkAll" onclick="CheckAll(this.form)" value="全选"
                                                   type="checkbox"
                                                   class="colored-success checkbox-parent" type="checkbox">
                                            <span class="text">全选</span>
                                            <span class="text text-danger">为{$authGroups.title}分配权限</span>
                                        </label>
                                    </th>
                                </tr>
                                </thead>
                                <tbody>
                                {foreach name="data" item="vo"}
                                <tr>
                                    <td>
                                        <label style="padding-right:15px;">
                                            <input id="{$vo.id}" value="{$vo.id}" name="rules[]"
                                                   dataid="id-{$vo.id}" <?php if(in_array($vo['id'], $rules)){ echo 'checked="checked"';}?>
                                            class="colored-success checkbox-parent" type="checkbox">
                                            <span class="text">{$vo.title}</span>
                                        </label>
                                    </td>
                                </tr>
                                {foreach name="vo['children']" item="vo2"}
                                <tr>
                                    <td style="padding-left:30px;">
                                        <label style="padding-right:15px;">
                                            <input <?php if(in_array($vo2['id'], $rules)){ echo 'checked="checked"';}?>
                                            class="colored-success checkbox-parent checkbox-child" id="{$vo2.id}"
                                            value="{$vo2.id}" name="rules[]" dataid="id-{$vo.id}-{$vo2.id}"
                                            type="checkbox">
                                            <span class="text">{$vo2.title}</span>
                                        </label>
                                    </td>
                                </tr>
                                {if condition="$vo2['children']"}
                                <tr>
                                    <td style="padding-left:60px;">
                                        {foreach name="vo2['children']" item="vo3"}
                                        <label style="padding-right:15px;">
                                            <input <?php if(in_array($vo3['id'], $rules)){ echo 'checked="checked"';}?>
                                            class="colored-success checkbox-child" id="{$vo3.id}" value="{$vo3.id}"
                                            name="rules[]" dataid="id-{$vo.id}-{$vo2.id}-{$vo3.id}" type="checkbox">
                                            <span class="text">{$vo3.title}</span>
                                        </label>
                                        {/foreach}
                                    </td>
                                </tr>
                                {/if}
                                {/foreach}
                                {/foreach}
                                </tbody>
                            </table>
                        </form>
                    </div>
                </div>
            </div>
        </section>
    </div>
    <!-- /#page-wrapper -->

</div>
<!-- /#wrapper -->

{include file="public/js"/}
<script src="https://cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script>
<script src="__STATIC__/lib/datatables/js/jquery.dataTables.min.js"></script>
<script src="__STATIC__/lib/datatables/js/dataTables.bootstrap.min.js"></script>
<script src="__STATIC__/lib/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="__STATIC__/lib/datatables-buttons/js/jszip.min.js"></script>
<script src="__STATIC__/lib/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="__STATIC__/lib/datatables-buttons/js/buttons.print.min.js"></script>

<script type="text/javascript">
    /* 多选判断 */
    function CheckAll(form) {
        for (var i = 0; i < form.elements.length; i++) {
            var e = form.elements[i];
            if (e.Name != 'chkAll' && e.disabled == false) {
                e.checked = form.chkAll.checked;
            }
        }
    }
    /* 权限配置 */
    $(function () {
        //动态选择框，上下级选中状态变化
        $('input.checkbox-parent').on('change', function () {
            var dataid = $(this).attr("dataid");
            $('input[dataid^=' + dataid + ']').prop('checked', $(this).is(':checked'));
        });
        $('input.checkbox-child').on('change', function () {
            var dataid = $(this).attr("dataid");
            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
            var parent = $('input[dataid=' + dataid + ']');
            if ($(this).is(':checked')) {
                parent.prop('checked', true);
                //循环到顶级
                while (dataid.lastIndexOf("-") != 2) {
                    dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                    parent = $('input[dataid=' + dataid + ']');
                    parent.prop('checked', true);
                }
            } else {
                //父级
                if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                    parent.prop('checked', false);
                    //循环到顶级
                    while (dataid.lastIndexOf("-") != 2) {
                        dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                        parent = $('input[dataid=' + dataid + ']');
                        if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                            parent.prop('checked', false);
                        }
                    }
                }
            }
        });
    });
</script>
</body>

</html>